using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Universal.Api.Attributes;
using Universal.Application.Contracts.Dto.Article;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class ArticleController : ControllerBase
{
    private readonly IArticleService _articleService;

    public ArticleController(IArticleService articleService)
    {
        _articleService = articleService;
    }

    /// <summary>
    /// 获取文章分页列表
    /// </summary>
    [HttpGet("paged")]
    public async Task<IActionResult> GetArticlesPagedAsync([FromQuery] int pageIndex, [FromQuery] int pageSize)
    {
        var result = await _articleService.GetArticlesPagedAsync(pageIndex, pageSize);
        return Ok(result);
    }

    /// <summary>
    /// 根据条件查询文章
    /// </summary>
    [HttpGet("query")]
    public async Task<IActionResult> QueryArticlesAsync([FromQuery] ArticleQueryDto query)
    {
        var result = await _articleService.QueryArticlesAsync(query);
        return Ok(result);
    }

    /// <summary>
    /// 获取单个文章详情
    /// </summary>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetArticleByIdAsync([FromRoute] Guid id)
    {
        var result = await _articleService.GetArticleByIdAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 创建文章
    /// </summary>
    [HttpPost]
    public async Task<IActionResult> CreateArticleAsync([FromBody] CreateArticleDto dto, [FromQuery] Guid userId)
    {
        // 添加详细的调试日志
        System.Diagnostics.Debug.WriteLine($"=== 控制器接收到数据 ===");
        System.Diagnostics.Debug.WriteLine($"Title: {dto?.Title}");
        System.Diagnostics.Debug.WriteLine($"Content: {dto?.Content}");
        System.Diagnostics.Debug.WriteLine($"CategoryIds Count: {dto?.CategoryIds?.Count ?? 0}");
        if (dto?.CategoryIds != null)
        {
            foreach (var id in dto.CategoryIds)
            {
                System.Diagnostics.Debug.WriteLine($"  CategoryId: {id}");
            }
        }
        System.Diagnostics.Debug.WriteLine($"CategoryId: {dto?.CategoryId}");

        // 打印原始请求数据
        Request.EnableBuffering();
        Request.Body.Position = 0;
        using var reader = new StreamReader(Request.Body);
        var requestBody = await reader.ReadToEndAsync();
        // System.Diagnostics.Debug.WriteLine($"原始请求体: {requestBody}");
        Request.Body.Position = 0;
        // System.Diagnostics.Debug.WriteLine($"========================");

#pragma warning disable CS8604 // 引用类型参数可能为 null。
        var result = await _articleService.CreateArticleAsync(dto, userId);
#pragma warning restore CS8604 // 引用类型参数可能为 null。
        return Ok(result);
    }

    /// <summary>
    /// 更新文章
    /// </summary>
    [HttpPut]
    public async Task<IActionResult> UpdateArticleAsync([FromBody] UpdateArticleDto dto)
    {
        var result = await _articleService.UpdateArticleAsync(dto);
        return Ok(result);
    }

    /// <summary>
    /// 删除文章
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteArticleAsync([FromRoute] Guid id)
    {
        var result = await _articleService.DeleteArticleAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 更新文章状态
    /// </summary>
    [HttpPut("{id}/status")]
    [RequirePermission("article.Edit")]
    public async Task<IActionResult> UpdateArticleStatusAsync([FromRoute] Guid id, [FromQuery] int status)
    {
        var result = await _articleService.UpdateArticleStatusAsync(id, status);
        return Ok(result);
    }
}
